package function;

import View.ForgetWindow;
import View.LoginWindow;
import View.window;
import client.ClientManager;
import instruction.Forget;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.regex.Pattern;
import instruction.*;
public class ForgetFunction {
    private static String SendYzm;
    public static Object $(window window, String id) {
        return (Object) window.getRoot().lookup("#" + id);
    }
    public static void forgetExec(ForgetWindow forgetWindow, LoginWindow loginWindow,ClientManager clientManager){
        ((Button)$(forgetWindow,"goButton")).setOnAction(actionEvent -> {
            forgetWindow.resetErrorTip();
            String correctPsw = "^[a-z,A-Z,0-9]{6,20}$";
            String correctRePsw = "^[a-z,A-Z,0-9]{6,20}$";
            String correctEmail = "^[a-zA-Z1-9_]\\w+@\\w+\\.(com|cn)";
            String account = ((TextField) $(forgetWindow, "actText")).getText();
            String psw = ((PasswordField) $(forgetWindow, "newPswText")).getText();
            String rePsw = ((PasswordField) $(forgetWindow, "rePswText")).getText();
            String yzm = ((TextField) $(forgetWindow, "yzmText")).getText();
            if (account.equals("") || psw.equals("") || rePsw.equals("") || yzm.equals("")) {
                if (account.equals("")) {
                    forgetWindow.setErrorTip("actTip", "！未输入邮箱");
                }
                if (psw.equals("")) {
                    forgetWindow.setErrorTip("newPswTip", "！未输入密码");
                }
                if (rePsw.equals("")) {
                    forgetWindow.setErrorTip("rePswTip", "！未输入密码");
                }
                if (yzm.equals("")) {
                    forgetWindow.setErrorTip("yzmTip", "！未输入验证码");
                }
            } else if (!Pattern.matches(correctEmail, account) || !Pattern.matches(correctPsw, psw) || !Pattern.matches(correctRePsw, rePsw)) {

                if (!Pattern.matches(correctEmail, account)) {
                    forgetWindow.setErrorTip("actTip", "邮箱格式错误");
                }
                if (!Pattern.matches(correctPsw, psw)) {
                    forgetWindow.setErrorTip("pswTip", "密码是长度在6-20位的英文字母和数字");
                }
                if (!Pattern.matches(correctRePsw, rePsw)) {
                    forgetWindow.setErrorTip("rePswTip", "密码是长度在6-20位的英文字母和数字");
                }
            } else if (!psw.equals(rePsw)) {
                forgetWindow.setErrorTip("pswTip", "两次密码不一致");
                forgetWindow.setErrorTip("rePswTip", "两次密码不一致");
            } else {
                if(SendYzm.equals(yzm)) {
                    try {
                            instruction.Message message=new instruction.Message("forget");
                            message.setForget(new Forget(psw,account));
                            clientManager.getSend().send(message);
                             loginWindow.show();
                             forgetWindow.hide();
                             send_email(account, "您的密码为 " + psw);
                    } catch (AddressException e) {
                        e.printStackTrace();
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                }else{
                    forgetWindow.setErrorTip("yzmTip","验证码错误");
                }
            }
        });
        ((Button)$(forgetWindow,"getYzmButton")).setOnAction(actionEvent -> {
            forgetWindow.resetErrorTip();
            String correctPsw = "^[a-z,A-Z,0-9]{6,20}$";
            String correctRePsw = "^[a-z,A-Z,0-9]{6,20}$";
            String correctEmail = "^[a-zA-Z1-9_]\\w+@\\w+\\.(com|cn)";
            String account = ((TextField) $(forgetWindow, "actText")).getText();
            String psw = ((PasswordField) $(forgetWindow, "newPswText")).getText();
            String rePsw = ((PasswordField) $(forgetWindow, "rePswText")).getText();
            String yzm = ((TextField) $(forgetWindow, "yzmText")).getText();
            if (account.equals("") || psw.equals("") || rePsw.equals("")) {
                if (account.equals("")) {
                    forgetWindow.setErrorTip("actTip", "！未输入邮箱");
                }
                if (psw.equals("")) {
                    forgetWindow.setErrorTip("newPswTip", "！未输入密码");
                }
                if (rePsw.equals("")) {
                    forgetWindow.setErrorTip("rePswTip", "！未输入密码");
                }
            } else if (!Pattern.matches(correctEmail, account) || !Pattern.matches(correctPsw, psw) || !Pattern.matches(correctRePsw, rePsw)) {

                if (!Pattern.matches(correctEmail, account)) {
                    forgetWindow.setErrorTip("actTip", "邮箱格式错误");
                }
                if (!Pattern.matches(correctPsw, psw)) {
                    forgetWindow.setErrorTip("pswTip", "密码是长度在6-20位的英文字母和数字");
                }
                if (!Pattern.matches(correctRePsw, rePsw)) {
                    forgetWindow.setErrorTip("rePswTip", "密码是长度在6-20位的英文字母和数字");
                }
            } else if (!psw.equals(rePsw)) {
                forgetWindow.setErrorTip("pswTip", "两次密码不一致");
                forgetWindow.setErrorTip("rePswTip", "两次密码不一致");
            } else {
                SendYzm=randomCode();
                try {
                    send_email(account,SendYzm);
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    //获取验证码
    public static String randomCode() {
        return (int)((Math.random() * 9 + 1) * 100000) + "";
    }
    public static void send_email(String receiver, String yzm) throws AddressException, MessagingException {
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.port", 465);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");
        Session session = Session.getInstance(properties);
        session.setDebug(true);
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("1623427947@qq.com"));
        InternetAddress to=new InternetAddress(receiver);
        message.setRecipient(MimeMessage.RecipientType.TO,to);
        message.setSubject("Let' chat!");
        message.setText(yzm);
        Transport transport = session.getTransport();
        transport.connect("1623427947@qq.com", "rqgwzfyehksscice");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}
